package com.kgis.fm.business.access.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kgis.fm.access.common.service.impl.AbstractExecutiveAccessServiceImpl;
import com.kgis.fm.business.access.model.Connectivity;
import com.kgis.fm.business.access.service.ConnectivityAccessService;
import com.kgis.fm.business.access.service.support.ConnectivityMapper;
/**
 * ConnectivityAccessService接口实现类
 * @author zhxiaomiao
 *
 */
public class ConnectivityAccessServiceImpl extends AbstractExecutiveAccessServiceImpl<Connectivity> implements ConnectivityAccessService<Connectivity> {
	private static final transient Log log = LogFactory.getLog(ConnectivityAccessServiceImpl.class);

	@Override
	public Connectivity findByFnoFid(int fno, long fid) {
		if (log.isInfoEnabled()) {
			log.info("数据库中查询connectivity_n" + " fno:" + fno + " fid:" + fid);
		}
		String sqlFindNode = "select * from connectivity_n where KGM_FNO=? AND KGM_FID=?";

		List<Connectivity> resultNode = getJdbcTemplateDataAccessObject().getJdbcTemplate().query(sqlFindNode, new Object[] { fno, fid }, new ConnectivityMapper());
		if (log.isInfoEnabled()) {
			log.info("根据fno、fid查询出结果集：" + resultNode);
		}
		if (resultNode == null || resultNode.size()==0) {
			return null;
		} else {
			if (resultNode.size() > 1) {
				if (log.isErrorEnabled()) {
					log.error("数据库中查询connectivity_n" + " fno:" + fno + " fid:" + fid + "结果集大于一条，请检查数据库");
				}
			}
			return resultNode.get(0);
		}

	}

	@Override
	public List<Connectivity> findByIds(long[] ids) {
		return null;
	}

	@Override
	public List<Connectivity> findRelatedFeaturesByFnoFid(int fno, long fid) {
		Connectivity connectivity = findByFnoFid(fno, fid);
		if (connectivity == null) {
			return null;
		}
		long node1Value = connectivity.getNode1();
		long node2Value = connectivity.getNode2();
		if (node1Value == 0 && node2Value == 0) {
			return null;
		}
		List<Connectivity> result=findRelatedFeatureByNodes(node1Value,node2Value,fid);
		if (log.isInfoEnabled()) {
			log.info("根据fno、fid查询出有关联关系的结果集：" + result);
		}
		return result;
	}

	public List<Connectivity> findRelatedFeatureByNodes(long node1Value,long node2Value,long fid){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT X.KGM_ID,X.KGM_FNO,X.KGM_FID,X.KGM_CNO,X.KGM_CID,X.NODE1_ID,X.NODE2_ID,X.ACTUAL_STATUS,X.USER_LENGTH FROM (");

		sql.append(" SELECT KGM_ID,KGM_FNO,KGM_FID,KGM_CNO,KGM_CID,NODE1_ID,NODE2_ID,ACTUAL_STATUS,USER_LENGTH,");
		sql.append(" ROW_NUMBER() OVER(PARTITION BY KGM_FID, KGM_FNO, KGM_CNO ORDER BY KGM_CID) RNUM");
		sql.append(" FROM CONNECTIVITY_N");
		sql.append(" WHERE (");
		if (node1Value > 0) {
			sql.append(" NODE1_ID = " + node1Value + " OR NODE2_ID = " + node1Value);
		}
		if(node1Value>0 && node2Value>0)
		{
			sql.append(" OR ");
		}
		if (node2Value > 0) {
			sql.append(" NODE1_ID = " + node2Value + " OR NODE2_ID = " + node2Value);
		}
		sql.append(") AND KGM_FID <>  ?");
		sql.append(" ) X WHERE X.RNUM = 1");
		List<Connectivity> result=getJdbcTemplateDataAccessObject().getJdbcTemplate().query(sql.toString(), new Object[] { fid }, new ConnectivityMapper());
		return result;
	}

}
